home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / LITTLE / P3SRC.ZIP / ATARI / MESH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-17  |  39.9 KB  |  2,152 lines

  1. /****************************************************************************
  2. *                mesh.c
  3. *
  4. *  This module implements primitives for mesh objects.
  5. *
  6. *  This module was written by Dieter Bayer [DB].
  7. *
  8. *  from Persistence of Vision(tm) Ray Tracer
  9. *  Copyright 1996 Persistence of Vision Team
  10. *---------------------------------------------------------------------------
  11. *  NOTICE: This source code file is provided so that users may experiment
  12. *  with enhancements to POV-Ray and to port the software to platforms other
  13. *  than those supported by the POV-Ray Team.  There are strict rules under
  14. *  which you are permitted to use this file.  The rules are in the file
  15. *  named POVLEGAL.DOC which should be distributed with this file. If
  16. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  17. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  18. *  Forum.  The latest version of POV-Ray may be found there as well.
  19. *
  20. * This program is based on the popular DKB raytracer version 2.12.
  21. * DKBTrace was originally written by David K. Buck.
  22. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  23. *
  24. *****************************************************************************/
  25.  
  26. /****************************************************************************
  27. *
  28. *  Explanation:
  29. *
  30. *    -
  31. *
  32. *  Syntax:
  33. *
  34. *    mesh
  35. *    {
  36. *      triangle { <CORNER1>, <CORNER2>, <CORNER3>, texture { NAME } }
  37. *      smooth_triangle { <CORNER1>, <NORMAL1>, <CORNER2>, <NORMAL2>, <CORNER3>, <NORMAL3>, texture { NAME } }
  38. *      ...
  39. *      [ hierarchy FLAG ]
  40. *    }
  41. *
  42. *  ---
  43. *
  44. *  Feb 1995 : Creation. [DB]
  45. *
  46. *****************************************************************************/
  47.  
  48. #include "frame.h"
  49. #include "vector.h"
  50. #include "povproto.h"
  51. #include "bbox.h"
  52. #include "matrices.h"
  53. #include "objects.h"
  54. #include "mesh.h"
  55. #include "texture.h"
  56. #include "povray.h"
  57.  
  58.  
  59.  
  60. /*****************************************************************************
  61. * Local preprocessor defines
  62. ******************************************************************************/
  63.  
  64. #define DEPTH_TOLERANCE 1e-6
  65.  
  66. #define max3_coordinate(x,y,z) ((x > y) ? ((x > z) ? X : Z) : ((y > z) ? Y : Z))
  67.  
  68. #define HASH_SIZE 1000
  69.  
  70. #define INITIAL_NUMBER_OF_ENTRIES 256
  71.  
  72.  
  73. /*****************************************************************************
  74. * Local typedefs
  75. ******************************************************************************/
  76.  
  77. typedef struct Hash_Table_Struct HASH_TABLE;
  78.  
  79. struct Hash_Table_Struct
  80. {
  81.   int Index;
  82.   SNGL_VECT P;
  83.   HASH_TABLE *Next;
  84. };
  85.  
  86.  
  87.  
  88. /*****************************************************************************
  89. * Static functions
  90. ******************************************************************************/
  91.  
  92. static int Intersect_Mesh  PARAMS((RAY *Ray, MESH *Mesh, ISTACK *Depth_Stack));
  93. static int All_Mesh_Intersections  PARAMS((OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack));
  94. static int Inside_Mesh  PARAMS((VECTOR IPoint, OBJECT *Object));
  95. static void Mesh_Normal  PARAMS((VECTOR Result, OBJECT *Object, INTERSECTION *Inter));
  96. static void *Copy_Mesh  PARAMS((OBJECT *Object));
  97. static void Translate_Mesh  PARAMS((OBJECT *Object, VECTOR Vector, TRANSFORM *Trans));
  98. static void Rotate_Mesh  PARAMS((OBJECT *Object, VECTOR Vector, TRANSFORM *Trans));
  99. static void Scale_Mesh  PARAMS((OBJECT *Object, VECTOR Vector, TRANSFORM *Trans));
  100. static void Transform_Mesh  PARAMS((OBJECT *Object, TRANSFORM *Trans));
  101. static void Invert_Mesh  PARAMS((OBJECT *Object));
  102. static void Destroy_Mesh  PARAMS((OBJECT *Object));
  103.  
  104. static void compute_smooth_triangle PARAMS((MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3));
  105. static int intersect_mesh_triangle PARAMS((RAY *Ray, MESH *Mesh, MESH_TRIANGLE *Triangle, DBL *Depth));
  106. static int test_hit PARAMS((MESH_TRIANGLE *Triangle, MESH *Mesh, RAY *Ray, DBL Depth, ISTACK *Depth_Stack));
  107. static void smooth_mesh_normal PARAMS((MESH *Mesh, VECTOR Result, MESH_TRIANGLE *Triangle, VECTOR IPoint));
  108. static void get_triangle_bbox PARAMS((MESH *Mesh, MESH_TRIANGLE *Triangle, BBOX *BBox));
  109.  
  110. static int intersect_bbox_tree PARAMS((MESH *Mesh, RAY *Ray, RAY *Orig_Ray, DBL len, ISTACK *Depth_Stack));
  111.  
  112. static void get_triangle_vertices PARAMS((MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3));
  113. static void get_triangle_normals PARAMS((MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR N1, VECTOR N2, VECTOR N3));
  114.  
  115. static int mesh_hash PARAMS((HASH_TABLE **Hash_Table,
  116.   int *Number, int *Max, SNGL_VECT **Elements, VECTOR aPoint));
  117.  
  118.  
  119.  
  120. /*****************************************************************************
  121. * Local variables
  122. ******************************************************************************/
  123.  
  124. METHODS Mesh_Methods =
  125. {
  126.   All_Mesh_Intersections,
  127.   Inside_Mesh, Mesh_Normal,
  128.   Copy_Mesh,
  129.   Translate_Mesh, Rotate_Mesh,
  130.   Scale_Mesh, Transform_Mesh, Invert_Mesh, Destroy_Mesh
  131. };
  132.  
  133. static HASH_TABLE **Vertex_Hash_Table, **Normal_Hash_Table;
  134.  
  135. static PRIORITY_QUEUE *Mesh_Queue;
  136.  
  137.  
  138.  
  139. /*****************************************************************************
  140. *
  141. * FUNCTION
  142. *
  143. *   All_Mesh_Intersections
  144. *
  145. * INPUT
  146. *   
  147. * OUTPUT
  148. *   
  149. * RETURNS
  150. *   
  151. * AUTHOR
  152. *
  153. *   Dieter Bayer
  154. *   
  155. * DESCRIPTION
  156. *
  157. *   -
  158. *
  159. * CHANGES
  160. *
  161. *   Feb 1995 : Creation.
  162. *
  163. ******************************************************************************/
  164.  
  165. static int All_Mesh_Intersections(Object, Ray, Depth_Stack)
  166. OBJECT *Object;
  167. RAY *Ray;
  168. ISTACK *Depth_Stack;
  169. {
  170.   Increase_Counter(stats[Ray_Mesh_Tests]);
  171.  
  172.   if (Intersect_Mesh(Ray, (MESH *)Object, Depth_Stack))
  173.   {
  174.     Increase_Counter(stats[Ray_Mesh_Tests_Succeeded]);
  175.  
  176.     return(TRUE);
  177.   }
  178.  
  179.   return(FALSE);
  180. }
  181.  
  182.  
  183.  
  184. /*****************************************************************************
  185. *
  186. * FUNCTION
  187. *
  188. *   Intersect_Mesh
  189. *
  190. * INPUT
  191. *   
  192. * OUTPUT
  193. *   
  194. * RETURNS
  195. *   
  196. * AUTHOR
  197. *
  198. *   Dieter Bayer
  199. *   
  200. * DESCRIPTION
  201. *
  202. *   -
  203. *
  204. * CHANGES
  205. *
  206. *   Feb 1995 : Creation.
  207. *
  208. ******************************************************************************/
  209.  
  210. static int Intersect_Mesh(Ray, Mesh, Depth_Stack)
  211. RAY *Ray;
  212. MESH *Mesh;
  213. ISTACK *Depth_Stack;
  214. {
  215.   int i;
  216.   unsigned found;
  217.   DBL len, t;
  218.   RAY New_Ray;
  219.  
  220.   /* Transform the ray into mesh space. */
  221.  
  222.   if (Mesh->Trans != NULL)
  223.   {
  224.     MInvTransPoint(New_Ray.Initial, Ray->Initial, Mesh->Trans);
  225.     MInvTransDirection(New_Ray.Direction, Ray->Direction, Mesh->Trans);
  226.  
  227.     VLength(len, New_Ray.Direction);
  228.     VInverseScaleEq(New_Ray.Direction, len);
  229.   }
  230.   else
  231.   {
  232.     New_Ray = *Ray;
  233.  
  234.     len = 1.0;
  235.   }
  236.  
  237.   found = FALSE;
  238.  
  239.   if (Mesh->Data->Tree == NULL)
  240.   {
  241.     /* There's no bounding hierarchy so just step through all elements. */
  242.  
  243.     for (i = 0; i < Mesh->Data->Number_Of_Triangles; i++)
  244.     {
  245.       if (intersect_mesh_triangle(&New_Ray, Mesh, &Mesh->Data->Triangles[i], &t))
  246.       {
  247.         if (test_hit(&Mesh->Data->Triangles[i], Mesh, Ray, t / len, Depth_Stack))
  248.         {
  249.           found = TRUE;
  250.         }
  251.       }
  252.     }
  253.   }
  254.   else
  255.   {
  256.     /* Use the mesh's bounding hierarchy. */
  257.  
  258.     return(intersect_bbox_tree(Mesh, &New_Ray, Ray, len, Depth_Stack));
  259.   }
  260.  
  261.   return(found);
  262. }
  263.  
  264.  
  265.  
  266. /*****************************************************************************
  267. *
  268. * FUNCTION
  269. *
  270. *   Inside_Mesh
  271. *
  272. * INPUT
  273. *   
  274. * OUTPUT
  275. *   
  276. * RETURNS
  277. *   
  278. * AUTHOR
  279. *
  280. *   Dieter Bayer
  281. *   
  282. * DESCRIPTION
  283. *
  284. *   -
  285. *
  286. * CHANGES
  287. *
  288. *   Feb 1995 : Creation.
  289. *
  290. ******************************************************************************/
  291.  
  292. static int Inside_Mesh(IPoint, Object)
  293. VECTOR IPoint;
  294. OBJECT *Object;
  295. {
  296.   return(FALSE);
  297. }
  298.  
  299.  
  300.  
  301. /*****************************************************************************
  302. *
  303. * FUNCTION
  304. *
  305. *   Mesh_Normal
  306. *
  307. * INPUT
  308. *   
  309. * OUTPUT
  310. *   
  311. * RETURNS
  312. *   
  313. * AUTHOR
  314. *
  315. *   Dieter Bayer
  316. *   
  317. * DESCRIPTION
  318. *
  319. *   Return the normalized normal in the given point.
  320. *
  321. * CHANGES
  322. *
  323. *   Feb 1995 : Creation.
  324. *
  325. ******************************************************************************/
  326.  
  327. static void Mesh_Normal(Result, Object, Inter)
  328. OBJECT *Object;
  329. VECTOR Result;
  330. INTERSECTION *Inter;
  331. {
  332.   VECTOR IPoint;
  333.   MESH_TRIANGLE *Triangle;
  334.   MESH *Mesh = (MESH *)Object;
  335.  
  336.   Triangle = (MESH_TRIANGLE *)Inter->Pointer;
  337.  
  338.   if (Triangle->Smooth)
  339.   {
  340.     if (Mesh->Trans != NULL)
  341.     {
  342.       MInvTransPoint(IPoint, Inter->IPoint, Mesh->Trans);
  343.     }
  344.     else
  345.     {
  346.       Assign_Vector(IPoint, Inter->IPoint);
  347.     }
  348.  
  349.     smooth_mesh_normal(Mesh, Result, Triangle, IPoint);
  350.   
  351.     if (Mesh->Trans != NULL)
  352.     {
  353.       MTransNormal(Result, Result, Mesh->Trans);
  354.     }
  355.  
  356.     VNormalize(Result, Result);
  357.   }
  358.   else
  359.   {
  360.     Assign_SNGL_Vect(Result, Mesh->Data->Normals[Triangle->Normal_Ind]);
  361.  
  362.     if (Mesh->Trans != NULL)
  363.     {
  364.       MTransNormal(Result, Result, Mesh->Trans);
  365.  
  366.       VNormalize(Result, Result);
  367.     }
  368.   }
  369. }
  370.  
  371.  
  372.  
  373. /*****************************************************************************
  374. *
  375. * FUNCTION
  376. *
  377. *   smooth_mesh_normal
  378. *
  379. * INPUT
  380. *   
  381. * OUTPUT
  382. *   
  383. * RETURNS
  384. *   
  385. * AUTHOR
  386. *
  387. *   Dieter Bayer
  388. *   
  389. * DESCRIPTION
  390. *
  391. *   Remove the un-normalized normal of a smoothed triangle.
  392. *
  393. * CHANGES
  394. *
  395. *   Feb 1995 : Creation. (Derived from TRIANGLE.C)
  396. *
  397. ******************************************************************************/
  398.  
  399. static void smooth_mesh_normal(Mesh, Result, Triangle, IPoint)
  400. MESH *Mesh;
  401. VECTOR Result, IPoint;
  402. MESH_TRIANGLE *Triangle;
  403. {
  404.   int axis;
  405.   DBL u, v;
  406.   DBL k1, k2, k3;
  407.   VECTOR PIMinusP1, N1, N2, N3;
  408.  
  409.   get_triangle_normals(Mesh, Triangle, N1, N2, N3);
  410.  
  411.   VSub(PIMinusP1, IPoint, Mesh->Data->Vertices[Triangle->P1]);
  412.  
  413.   VDot(u, PIMinusP1, Triangle->Perp);
  414.  
  415.   if (u < EPSILON)
  416.   {
  417.     Assign_Vector(Result, N1);
  418.   }
  419.   else
  420.   {
  421.     axis = Triangle->vAxis;
  422.  
  423.     k1 = Mesh->Data->Vertices[Triangle->P1][axis];
  424.     k2 = Mesh->Data->Vertices[Triangle->P2][axis];
  425.     k3 = Mesh->Data->Vertices[Triangle->P3][axis];
  426.  
  427.     v = (PIMinusP1[axis] / u + k1 - k2) / (k3 - k2);
  428.  
  429.     Result[X] = N1[X] + u * (N2[X] - N1[X] + v * (N3[X] - N2[X]));
  430.     Result[Y] = N1[Y] + u * (N2[Y] - N1[Y] + v * (N3[Y] - N2[Y]));
  431.     Result[Z] = N1[Z] + u * (N2[Z] - N1[Z] + v * (N3[Z] - N2[Z]));
  432.  
  433.   }
  434.  
  435. }
  436.  
  437.  
  438.  
  439. /*****************************************************************************
  440. *
  441. * FUNCTION
  442. *
  443. *   Translate_Mesh
  444. *
  445. * INPUT
  446. *   
  447. * OUTPUT
  448. *   
  449. * RETURNS
  450. *   
  451. * AUTHOR
  452. *
  453. *   Dieter Bayer
  454. *   
  455. * DESCRIPTION
  456. *
  457. *   -
  458. *
  459. * CHANGES
  460. *
  461. *   Feb 1995 : Creation.
  462. *
  463. ******************************************************************************/
  464.  
  465. static void Translate_Mesh(Object, Vector, Trans)
  466. OBJECT *Object;
  467. VECTOR Vector;
  468. TRANSFORM *Trans;
  469. {
  470.   Transform_Mesh(Object, Trans);
  471. }
  472.  
  473.  
  474.  
  475. /*****************************************************************************
  476. *
  477. * FUNCTION
  478. *
  479. *   Rotate_Mesh
  480. *
  481. * INPUT
  482. *   
  483. * OUTPUT
  484. *   
  485. * RETURNS
  486. *   
  487. * AUTHOR
  488. *
  489. *   Dieter Bayer
  490. *   
  491. * DESCRIPTION
  492. *
  493. *   -
  494. *
  495. * CHANGES
  496. *
  497. *   Feb 1995 : Creation.
  498. *
  499. ******************************************************************************/
  500.  
  501. static void Rotate_Mesh(Object, Vector, Trans)
  502. OBJECT *Object;
  503. VECTOR Vector;
  504. TRANSFORM *Trans;
  505. {
  506.   Transform_Mesh(Object, Trans);
  507. }
  508.  
  509.  
  510.  
  511. /*****************************************************************************
  512. *
  513. * FUNCTION
  514. *
  515. *   Scale_Mesh
  516. *
  517. * INPUT
  518. *   
  519. * OUTPUT
  520. *   
  521. * RETURNS
  522. *   
  523. * AUTHOR
  524. *
  525. *   Dieter Bayer
  526. *   
  527. * DESCRIPTION
  528. *
  529. *   -
  530. *
  531. * CHANGES
  532. *
  533. *   Feb 1995 : Creation.
  534. *
  535. ******************************************************************************/
  536.  
  537. static void Scale_Mesh(Object, Vector, Trans)
  538. OBJECT *Object;
  539. VECTOR Vector;
  540. TRANSFORM *Trans;
  541. {
  542.   Transform_Mesh(Object, Trans);
  543. }
  544.  
  545.  
  546.  
  547. /*****************************************************************************
  548. *
  549. * FUNCTION
  550. *
  551. *   Transfrom_Mesh
  552. *
  553. * INPUT
  554. *   
  555. * OUTPUT
  556. *   
  557. * RETURNS
  558. *   
  559. * AUTHOR
  560. *
  561. *   Dieter Bayer
  562. *   
  563. * DESCRIPTION
  564. *
  565. *   -
  566. *
  567. * CHANGES
  568. *
  569. *   Feb 1995 : Creation.
  570. *
  571. ******************************************************************************/
  572.  
  573. static void Transform_Mesh(Object, Trans)
  574. OBJECT *Object;
  575. TRANSFORM *Trans;
  576. {
  577.   if (((MESH *)Object)->Trans == NULL)
  578.   {
  579.     ((MESH *)Object)->Trans = Create_Transform();
  580.   }
  581.  
  582.   Recompute_BBox(&Object->BBox, Trans);
  583.  
  584.   Compose_Transforms(((MESH *)Object)->Trans, Trans);
  585. }
  586.  
  587.  
  588.  
  589. /*****************************************************************************
  590. *
  591. * FUNCTION
  592. *
  593. *   Invert_Mesh
  594. *
  595. * INPUT
  596. *   
  597. * OUTPUT
  598. *   
  599. * RETURNS
  600. *   
  601. * AUTHOR
  602. *
  603. *   Dieter Bayer
  604. *   
  605. * DESCRIPTION
  606. *
  607. *   -
  608. *
  609. * CHANGES
  610. *
  611. *   Feb 1995 : Creation.
  612. *
  613. ******************************************************************************/
  614.  
  615. static void Invert_Mesh(Object)
  616. OBJECT *Object;
  617. {
  618. }
  619.  
  620.  
  621.  
  622. /*****************************************************************************
  623. *
  624. * FUNCTION
  625. *
  626. *   Create_Mesh
  627. *
  628. * INPUT
  629. *   
  630. * OUTPUT
  631. *   
  632. * RETURNS
  633. *   
  634. * AUTHOR
  635. *
  636. *   Dieter Bayer
  637. *   
  638. * DESCRIPTION
  639. *
  640. *   -
  641. *
  642. * CHANGES
  643. *
  644. *   Feb 1995 : Creation.
  645. *
  646. ******************************************************************************/
  647.  
  648. MESH *Create_Mesh()
  649. {
  650.   MESH *New;
  651.  
  652.   New = (MESH *)POV_MALLOC(sizeof(MESH), "mesh");
  653.  
  654.   INIT_OBJECT_FIELDS(New,MESH_OBJECT,&Mesh_Methods)
  655.  
  656.   Set_Flag(New, HIERARCHY_FLAG);
  657.  
  658.   New->Trans = NULL;
  659.  
  660.   New->Data = NULL;
  661.  
  662.   return(New);
  663. }
  664.  
  665.  
  666.  
  667. /*****************************************************************************
  668. *
  669. * FUNCTION
  670. *
  671. *   Copy_Mesh
  672. *
  673. * INPUT
  674. *   
  675. * OUTPUT
  676. *   
  677. * RETURNS
  678. *   
  679. * AUTHOR
  680. *
  681. *   Dieter Bayer
  682. *   
  683. * DESCRIPTION
  684. *
  685. *   Copy a mesh.
  686. *
  687. *   NOTE: The components are not copied, only the number of references is
  688. *         counted, so that Destroy_Mesh() knows if they can be destroyed.
  689. *
  690. *   -
  691. *
  692. * CHANGES
  693. *
  694. *   Feb 1995 : Creation.
  695. *
  696. ******************************************************************************/
  697.  
  698. static void *Copy_Mesh(Object)
  699. OBJECT *Object;
  700. {
  701.   MESH *New;
  702.  
  703.   New = Create_Mesh();
  704.  
  705.   /* Copy mesh. */
  706.  
  707.   *New = *((MESH *)Object);
  708.   
  709.   New->Trans = Copy_Transform(New->Trans);
  710.   
  711.   New->Data->References++;
  712.  
  713.   return(New);
  714. }
  715.  
  716.  
  717.  
  718. /*****************************************************************************
  719. *
  720. * FUNCTION
  721. *
  722. *   Destroy_Mesh
  723. *
  724. * INPUT
  725. *   
  726. * OUTPUT
  727. *   
  728. * RETURNS
  729. *   
  730. * AUTHOR
  731. *
  732. *   Dieter Bayer
  733. *   
  734. * DESCRIPTION
  735. *
  736. *   -
  737. *
  738. * CHANGES
  739. *
  740. *   Feb 1995 : Creation.
  741. *
  742. ******************************************************************************/
  743.  
  744. static void Destroy_Mesh(Object)
  745. OBJECT *Object;
  746. {
  747.   int i;
  748.   MESH *Mesh = (MESH *)Object;
  749.  
  750.   Destroy_Transform(Mesh->Trans);
  751.  
  752.   if (--(Mesh->Data->References) == 0)
  753.   {
  754.     Destroy_BBox_Tree(Mesh->Data->Tree);
  755.  
  756.     if (Mesh->Data->Normals != NULL)
  757.     {
  758.       POV_FREE(Mesh->Data->Normals);
  759.     }
  760.  
  761.     if (Mesh->Data->Vertices != NULL)
  762.     {
  763.       POV_FREE(Mesh->Data->Vertices);
  764.     }
  765.  
  766.     if (Mesh->Data->Triangles != NULL)
  767.     {
  768.       POV_FREE(Mesh->Data->Triangles);
  769.     }
  770.  
  771.     if (Mesh->Data->Textures != NULL)
  772.     {
  773.       for (i = 0; i < Mesh->Data->Number_Of_Textures; i++)
  774.       {
  775.         Destroy_Textures(Mesh->Data->Textures[i]);
  776.       }
  777.  
  778.       POV_FREE(Mesh->Data->Textures);
  779.     }
  780.  
  781.     POV_FREE(Mesh->Data);
  782.   }
  783.  
  784.   POV_FREE(Object);
  785. }
  786.  
  787.  
  788.  
  789. /*****************************************************************************
  790. *
  791. * FUNCTION
  792. *
  793. *   Compute_Mesh_BBox
  794. *
  795. * INPUT
  796. *
  797. *   Mesh - Mesh
  798. *   
  799. * OUTPUT
  800. *
  801. *   Mesh
  802. *   
  803. * RETURNS
  804. *   
  805. * AUTHOR
  806. *
  807. *   Dieter Bayer
  808. *   
  809. * DESCRIPTION
  810. *
  811. *   Calculate the bounding box of a triangle.
  812. *
  813. * CHANGES
  814. *
  815. *   Feb 1995 : Creation.
  816. *
  817. ******************************************************************************/
  818.  
  819. void Compute_Mesh_BBox(Mesh)
  820. MESH *Mesh;
  821. {
  822.   int i;
  823.   VECTOR P1, P2, P3;
  824.   VECTOR mins, maxs;
  825.  
  826.   Make_Vector(mins, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
  827.   Make_Vector(maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
  828.  
  829.   for (i = 0; i < Mesh->Data->Number_Of_Triangles; i++)
  830.   {
  831.     get_triangle_vertices(Mesh, &Mesh->Data->Triangles[i], P1, P2, P3);
  832.  
  833.     mins[X] = min(mins[X], min3(P1[X], P2[X], P3[X]));
  834.     mins[Y] = min(mins[Y], min3(P1[Y], P2[Y], P3[Y]));
  835.     mins[Z] = min(mins[Z], min3(P1[Z], P2[Z], P3[Z]));
  836.  
  837.     maxs[X] = max(maxs[X], max3(P1[X], P2[X], P3[X]));
  838.     maxs[Y] = max(maxs[Y], max3(P1[Y], P2[Y], P3[Y]));
  839.     maxs[Z] = max(maxs[Z], max3(P1[Z], P2[Z], P3[Z]));
  840.   }
  841.  
  842.   Make_BBox_from_min_max(Mesh->BBox, mins, maxs);
  843. }
  844.  
  845.  
  846.  
  847. /*****************************************************************************
  848. *
  849. * FUNCTION
  850. *
  851. *   Compute_Mesh
  852. *
  853. * INPUT
  854. *   
  855. * OUTPUT
  856. *   
  857. * RETURNS
  858. *   
  859. * AUTHOR
  860. *
  861. *   Dieter Bayer
  862. *   
  863. * DESCRIPTION
  864. *
  865. *   -
  866. *
  867. * CHANGES
  868. *
  869. *   Feb 1995 : Creation.
  870. *
  871. ******************************************************************************/
  872.  
  873. int Compute_Mesh_Triangle(Triangle, Smooth, P1, P2, P3, S_Normal)
  874. MESH_TRIANGLE *Triangle;
  875. int Smooth;
  876. VECTOR P1, P2, P3, S_Normal;
  877. {
  878.   int temp, swap;
  879.   DBL x, y, z;
  880.   VECTOR V1, V2, T1;
  881.   DBL Length;
  882.  
  883.   VSub(V1, P2, P1);
  884.   VSub(V2, P3, P1);
  885.  
  886.   VCross(S_Normal, V2, V1);
  887.  
  888.   VLength(Length, S_Normal);
  889.  
  890.   /* Set up a flag so we can ignore degenerate triangles */
  891.  
  892.   if (Length == 0.0)
  893.   {
  894.     return(FALSE);
  895.   }
  896.  
  897.   /* Normalize the normal vector. */
  898.  
  899.   VInverseScaleEq(S_Normal, Length);
  900.  
  901.   VDot(Triangle->Distance, S_Normal, P1);
  902.  
  903.   Triangle->Distance *= -1.0;
  904.  
  905.   /* Find triangle's dominant axis. */
  906.  
  907.   x = fabs(S_Normal[X]);
  908.   y = fabs(S_Normal[Y]);
  909.   z = fabs(S_Normal[Z]);
  910.  
  911.   Triangle->Dominant_Axis = max3_coordinate(x, y, z);
  912.  
  913.   swap = FALSE;
  914.  
  915.   switch (Triangle->Dominant_Axis)
  916.   {
  917.     case X:
  918.  
  919.       if ((P2[Y] - P3[Y])*(P2[Z] - P1[Z]) < (P2[Z] - P3[Z])*(P2[Y] - P1[Y]))
  920.       {
  921.         swap = TRUE;
  922.       }
  923.  
  924.       break;
  925.  
  926.     case Y:
  927.  
  928.       if ((P2[X] - P3[X])*(P2[Z] - P1[Z]) < (P2[Z] - P3[Z])*(P2[X] - P1[X]))
  929.       {
  930.         swap = TRUE;
  931.       }
  932.  
  933.       break;
  934.  
  935.     case Z:
  936.  
  937.       if ((P2[X] - P3[X])*(P2[Y] - P1[Y]) < (P2[Y] - P3[Y])*(P2[X] - P1[X]))
  938.       {
  939.         swap = TRUE;
  940.       }
  941.  
  942.       break;
  943.   }
  944.  
  945.   if (swap)
  946.   {
  947.     temp = Triangle->P2;
  948.     Triangle->P2 = Triangle->P1;
  949.     Triangle->P1 = temp;
  950.  
  951.     Assign_Vector(T1, P1);
  952.     Assign_Vector(P1, P2);
  953.     Assign_Vector(P2, T1);
  954.  
  955.     if (Smooth)
  956.     {
  957.       temp = Triangle->N2;
  958.       Triangle->N2 = Triangle->N1;
  959.       Triangle->N1 = temp;
  960.     }
  961.   }
  962.  
  963.   if (Smooth)
  964.   {
  965.     compute_smooth_triangle(Triangle, P1, P2, P3);
  966.   }
  967.  
  968.   return(TRUE);
  969. }
  970.  
  971.  
  972.  
  973. /*****************************************************************************
  974. *
  975. * FUNCTION
  976. *
  977. *   compute_smooth_triangle
  978. *
  979. * INPUT
  980. *   
  981. * OUTPUT
  982. *   
  983. * RETURNS
  984. *   
  985. * AUTHOR
  986. *
  987. *   Dieter Bayer
  988. *   
  989. * DESCRIPTION
  990. *
  991. *   -
  992. *
  993. * CHANGES
  994. *
  995. *   Feb 1995 : Creation.
  996. *
  997. ******************************************************************************/
  998.  
  999. static void compute_smooth_triangle(Triangle, P1, P2, P3)
  1000. MESH_TRIANGLE *Triangle;
  1001. VECTOR P1, P2, P3;
  1002. {
  1003.   VECTOR P3MinusP2, VTemp1, VTemp2;
  1004.   DBL x, y, z, uDenominator, Proj;
  1005.  
  1006.   Triangle->Smooth = TRUE;
  1007.  
  1008.   VSub(P3MinusP2, P3, P2);
  1009.  
  1010.   x = fabs(P3MinusP2[X]);
  1011.   y = fabs(P3MinusP2[Y]);
  1012.   z = fabs(P3MinusP2[Z]);
  1013.  
  1014.   Triangle->vAxis = max3_coordinate(x, y, z);
  1015.  
  1016.   VSub(VTemp1, P2, P3);
  1017.  
  1018.   VNormalize(VTemp1, VTemp1);
  1019.  
  1020.   VSub(VTemp2, P1, P3);
  1021.  
  1022.   VDot(Proj, VTemp2, VTemp1);
  1023.  
  1024.   VScaleEq(VTemp1, Proj);
  1025.  
  1026.   VSub(Triangle->Perp, VTemp1, VTemp2);
  1027.  
  1028.   VNormalize(Triangle->Perp, Triangle->Perp);
  1029.  
  1030.   VDot(uDenominator, VTemp2, Triangle->Perp);
  1031.  
  1032.   VInverseScaleEq(Triangle->Perp, -uDenominator);
  1033. }
  1034.  
  1035.  
  1036.  
  1037. /*****************************************************************************
  1038. *
  1039. * FUNCTION
  1040. *
  1041. *   intersect_mesh_triangle
  1042. *
  1043. * INPUT
  1044. *   
  1045. * OUTPUT
  1046. *   
  1047. * RETURNS
  1048. *   
  1049. * AUTHOR
  1050. *
  1051. *   Dieter Bayer
  1052. *   
  1053. * DESCRIPTION
  1054. *
  1055. *   -
  1056. *
  1057. * CHANGES
  1058. *
  1059. *   Feb 1995 : Creation.
  1060. *
  1061. ******************************************************************************/
  1062.  
  1063. static int intersect_mesh_triangle(Ray, Mesh, Triangle, Depth)
  1064. RAY *Ray;
  1065. MESH *Mesh;
  1066. MESH_TRIANGLE *Triangle;
  1067. DBL *Depth;
  1068. {
  1069.   DBL NormalDotOrigin, NormalDotDirection;
  1070.   DBL s, t;
  1071.   VECTOR P1, P2, P3, S_Normal;
  1072.  
  1073.   Assign_SNGL_Vect(S_Normal, Mesh->Data->Normals[Triangle->Normal_Ind]);
  1074.  
  1075.   VDot(NormalDotDirection, S_Normal, Ray->Direction);
  1076.  
  1077.   if (fabs(NormalDotDirection) < EPSILON)
  1078.   {
  1079.     return(FALSE);
  1080.   }
  1081.  
  1082.   VDot(NormalDotOrigin, S_Normal, Ray->Initial);
  1083.  
  1084.   *Depth = -(Triangle->Distance + NormalDotOrigin) / NormalDotDirection;
  1085.  
  1086.   if ((*Depth < DEPTH_TOLERANCE) || (*Depth > Max_Distance))
  1087.   {
  1088.     return(FALSE);
  1089.   }
  1090.  
  1091.   get_triangle_vertices(Mesh, Triangle, P1, P2, P3);
  1092.  
  1093.   switch (Triangle->Dominant_Axis)
  1094.   {
  1095.     case X:
  1096.  
  1097.       s = Ray->Initial[Y] + *Depth * Ray->Direction[Y];
  1098.       t = Ray->Initial[Z] + *Depth * Ray->Direction[Z];
  1099.  
  1100.       if ((P2[Y] - s) * (P2[Z] - P1[Z]) < (P2[Z] - t) * (P2[Y] - P1[Y]))
  1101.       {
  1102.         return(FALSE);
  1103.       }
  1104.  
  1105.       if ((P3[Y] - s) * (P3[Z] - P2[Z]) < (P3[Z] - t) * (P3[Y] - P2[Y]))
  1106.       {
  1107.         return(FALSE);
  1108.       }
  1109.  
  1110.       if ((P1[Y] - s) * (P1[Z] - P3[Z]) < (P1[Z] - t) * (P1[Y] - P3[Y]))
  1111.       {
  1112.         return(FALSE);
  1113.       }
  1114.  
  1115.       return(TRUE);
  1116.  
  1117.     case Y:
  1118.  
  1119.       s = Ray->Initial[X] + *Depth * Ray->Direction[X];
  1120.       t = Ray->Initial[Z] + *Depth * Ray->Direction[Z];
  1121.  
  1122.       if ((P2[X] - s) * (P2[Z] - P1[Z]) < (P2[Z] - t) * (P2[X] - P1[X]))
  1123.       {
  1124.         return(FALSE);
  1125.       }
  1126.  
  1127.       if ((P3[X] - s) * (P3[Z] - P2[Z]) < (P3[Z] - t) * (P3[X] - P2[X]))
  1128.       {
  1129.         return(FALSE);
  1130.       }
  1131.  
  1132.       if ((P1[X] - s) * (P1[Z] - P3[Z]) < (P1[Z] - t) * (P1[X] - P3[X]))
  1133.       {
  1134.         return(FALSE);
  1135.       }
  1136.  
  1137.       return(TRUE);
  1138.  
  1139.     case Z:
  1140.  
  1141.       s = Ray->Initial[X] + *Depth * Ray->Direction[X];
  1142.       t = Ray->Initial[Y] + *Depth * Ray->Direction[Y];
  1143.  
  1144.       if ((P2[X] - s) * (P2[Y] - P1[Y]) < (P2[Y] - t) * (P2[X] - P1[X]))
  1145.       {
  1146.         return(FALSE);
  1147.       }
  1148.  
  1149.       if ((P3[X] - s) * (P3[Y] - P2[Y]) < (P3[Y] - t) * (P3[X] - P2[X]))
  1150.       {
  1151.         return(FALSE);
  1152.       }
  1153.  
  1154.       if ((P1[X] - s) * (P1[Y] - P3[Y]) < (P1[Y] - t) * (P1[X] - P3[X]))
  1155.       {
  1156.         return(FALSE);
  1157.       }
  1158.  
  1159.       return(TRUE);
  1160.   }
  1161.  
  1162.   return(FALSE);
  1163. }
  1164.  
  1165.  
  1166.  
  1167. /*****************************************************************************
  1168. *
  1169. * FUNCTION
  1170. *
  1171. *   test_hit
  1172. *
  1173. * INPUT
  1174. *   
  1175. * OUTPUT
  1176. *   
  1177. * RETURNS
  1178. *   
  1179. * AUTHOR
  1180. *
  1181. *   Dieter Bayer
  1182. *   
  1183. * DESCRIPTION
  1184. *
  1185. *   Test if a hit is valid and push if on the intersection depth.
  1186. *
  1187. * CHANGES
  1188. *
  1189. *   Feb 1995 : Creation.
  1190. *
  1191. ******************************************************************************/
  1192.  
  1193. static int test_hit(Triangle, Mesh, Ray, Depth, Depth_Stack)
  1194. MESH_TRIANGLE *Triangle;
  1195. MESH *Mesh;
  1196. RAY *Ray;
  1197. DBL Depth;
  1198. ISTACK *Depth_Stack;
  1199. {
  1200.   VECTOR IPoint;
  1201.   OBJECT *Object = (OBJECT *)Mesh;
  1202.  
  1203.   VEvaluateRay(IPoint, Ray->Initial, Depth, Ray->Direction);
  1204.  
  1205.   if (Point_In_Clip(IPoint, Object->Clip))
  1206.   {
  1207.     push_entry_pointer(Depth, IPoint, Object, Triangle, Depth_Stack);
  1208.  
  1209.     return(TRUE);
  1210.   }
  1211.  
  1212.   return(FALSE);
  1213. }
  1214.  
  1215.  
  1216.  
  1217. /*****************************************************************************
  1218. *
  1219. * FUNCTION
  1220. *
  1221. *   Init_Mesh_Triangle
  1222. *
  1223. * INPUT
  1224. *   
  1225. * OUTPUT
  1226. *   
  1227. * RETURNS
  1228. *   
  1229. * AUTHOR
  1230. *
  1231. *   Dieter Bayer
  1232. *   
  1233. * DESCRIPTION
  1234. *
  1235. *   -
  1236. *
  1237. * CHANGES
  1238. *
  1239. *   Feb 1995 : Creation.
  1240. *
  1241. ******************************************************************************/
  1242.  
  1243. void Init_Mesh_Triangle(Triangle)
  1244. MESH_TRIANGLE *Triangle;
  1245. {
  1246.   Triangle->Smooth = FALSE;
  1247.  
  1248.   Triangle->Dominant_Axis = 0;
  1249.   Triangle->vAxis         = 0;
  1250.  
  1251.   Triangle->P1 =
  1252.   Triangle->P2 =
  1253.   Triangle->P3 = -1;
  1254.  
  1255.   Triangle->Normal_Ind = -1;
  1256.  
  1257.   Triangle->Texture = -1;
  1258.  
  1259.   Triangle->N1 =
  1260.   Triangle->N2 =
  1261.   Triangle->N3 = -1;
  1262.  
  1263.   Make_Vector(Triangle->Perp, 0.0, 0.0, 0.0);
  1264.  
  1265.   Triangle->Distance = 0.0;
  1266. }
  1267.  
  1268.  
  1269.  
  1270. /*****************************************************************************
  1271. *
  1272. * FUNCTION
  1273. *
  1274. *   get_triangle_bbox
  1275. *
  1276. * INPUT
  1277. *
  1278. *   Triangle - Pointer to triangle
  1279. *   
  1280. * OUTPUT
  1281. *
  1282. *   BBox     - Bounding box
  1283. *   
  1284. * RETURNS
  1285. *   
  1286. * AUTHOR
  1287. *
  1288. *   Dieter Bayer
  1289. *   
  1290. * DESCRIPTION
  1291. *
  1292. *   Calculate the bounding box of a triangle.
  1293. *
  1294. * CHANGES
  1295. *
  1296. *   Sep 1994 : Creation.
  1297. *
  1298. ******************************************************************************/
  1299.  
  1300. static void get_triangle_bbox(Mesh, Triangle, BBox)
  1301. MESH *Mesh;
  1302. MESH_TRIANGLE *Triangle;
  1303. BBOX *BBox;
  1304. {
  1305.   VECTOR P1, P2, P3;
  1306.   VECTOR Min, Max;
  1307.  
  1308.   get_triangle_vertices(Mesh, Triangle, P1, P2, P3);
  1309.  
  1310.   Min[X] = min3(P1[X], P2[X], P3[X]);
  1311.   Min[Y] = min3(P1[Y], P2[Y], P3[Y]);
  1312.   Min[Z] = min3(P1[Z], P2[Z], P3[Z]);
  1313.  
  1314.   Max[X] = max3(P1[X], P2[X], P3[X]);
  1315.   Max[Y] = max3(P1[Y], P2[Y], P3[Y]);
  1316.   Max[Z] = max3(P1[Z], P2[Z], P3[Z]);
  1317.  
  1318.   Make_BBox_from_min_max(*BBox, Min, Max);
  1319. }
  1320.  
  1321.  
  1322.  
  1323. /*****************************************************************************
  1324. *
  1325. * FUNCTION
  1326. *
  1327. *   Build_Mesh_BBox_Tree
  1328. *
  1329. * INPUT
  1330. *   
  1331. * OUTPUT
  1332. *   
  1333. * RETURNS
  1334. *   
  1335. * AUTHOR
  1336. *
  1337. *   Dieter Bayer
  1338. *   
  1339. * DESCRIPTION
  1340. *
  1341. *   Create the bounding box hierarchy.
  1342. *
  1343. * CHANGES
  1344. *
  1345. *   Feb 1995 : Creation. (Derived from the bounding slab creation code)
  1346. *
  1347. ******************************************************************************/
  1348.  
  1349. void Build_Mesh_BBox_Tree(Mesh)
  1350. MESH *Mesh;
  1351. {
  1352.   int i, nElem, maxelements;
  1353.   BBOX_TREE **Triangles;
  1354.  
  1355.   if (!Test_Flag(Mesh, HIERARCHY_FLAG))
  1356.   {
  1357.     return;
  1358.   }
  1359.  
  1360.   nElem = (int)Mesh->Data->Number_Of_Triangles;
  1361.  
  1362.   maxelements = 2 * nElem;
  1363.  
  1364.   /* Now allocate an array to hold references to these elements. */
  1365.  
  1366.   Triangles = (BBOX_TREE **)POV_MALLOC(maxelements*sizeof(BBOX_TREE *), "mesh bbox tree");
  1367.  
  1368.   /* Init list with mesh elements. */
  1369.  
  1370.   for (i = 0; i < nElem; i++)
  1371.   {
  1372.     Triangles[i] = (BBOX_TREE *)POV_MALLOC(sizeof(BBOX_TREE), "mesh bbox tree");
  1373.  
  1374.     Triangles[i]->Infinite = FALSE;
  1375.     Triangles[i]->Entries  = 0;
  1376.     Triangles[i]->Node     = (BBOX_TREE **)&Mesh->Data->Triangles[i];
  1377.  
  1378.     get_triangle_bbox(Mesh, &Mesh->Data->Triangles[i], &Triangles[i]->BBox);
  1379.   }
  1380.  
  1381.   Build_BBox_Tree(&Mesh->Data->Tree, nElem, Triangles, 0, NULL);
  1382.  
  1383.   /* Get rid of the Triangles array. */
  1384.  
  1385.   POV_FREE(Triangles);
  1386. }
  1387.  
  1388.  
  1389.  
  1390. /*****************************************************************************
  1391. *
  1392. * FUNCTION
  1393. *
  1394. *   intersect_bbox_tree
  1395. *
  1396. * INPUT
  1397. *
  1398. *   Mesh     - Mesh object
  1399. *   Ray      - Current ray
  1400. *   Orig_Ray - Original, untransformed ray
  1401. *   len      - Length of the transformed ray direction
  1402. *   
  1403. * OUTPUT
  1404. *
  1405. *   Depth_Stack - Stack of intersections
  1406. *   
  1407. * RETURNS
  1408. *
  1409. *   int - TRUE if an intersection was found
  1410. *   
  1411. * AUTHOR
  1412. *
  1413. *   Dieter Bayer
  1414. *   
  1415. * DESCRIPTION
  1416. *
  1417. *   Intersect a ray with the bounding box tree of a mesh.
  1418. *
  1419. * CHANGES
  1420. *
  1421. *   Feb 1995 : Creation.
  1422. *
  1423. ******************************************************************************/
  1424.  
  1425. static int intersect_bbox_tree(Mesh, Ray, Orig_Ray, len, Depth_Stack)
  1426. MESH *Mesh;
  1427. RAY *Ray, *Orig_Ray;
  1428. DBL len;
  1429. ISTACK *Depth_Stack;
  1430. {
  1431.   int i, found;
  1432.   DBL Best, Depth;
  1433.   RAYINFO rayinfo;
  1434.   BBOX_TREE *Node, *Root;
  1435.  
  1436.   /* Create the direction vectors for this ray. */
  1437.  
  1438.   Create_Rayinfo(Ray, &rayinfo);
  1439.  
  1440.   /* Start with an empty priority queue. */
  1441.  
  1442.   found = 0;
  1443.  
  1444.   Mesh_Queue->QSize = 0;
  1445.  
  1446.   Best = BOUND_HUGE;
  1447.  
  1448. #ifdef BBOX_EXTRA_STATS
  1449.   Increase_Counter(stats[totalQueueResets]);
  1450. #endif
  1451.  
  1452.   /* Check top node. */
  1453.  
  1454.   Root = Mesh->Data->Tree;
  1455.  
  1456.   /* Set the root object infinite to avoid a test. */
  1457.  
  1458.   Check_And_Enqueue(Mesh_Queue, Root, &Root->BBox, &rayinfo);
  1459.  
  1460.   /* Check elements in the priority queue. */
  1461.  
  1462.   while (Mesh_Queue->QSize > 0)
  1463.   {
  1464.     Priority_Queue_Delete(Mesh_Queue, &Depth, &Node);
  1465.  
  1466.     /*
  1467.      * If current intersection is larger than the best intersection found
  1468.      * so far our task is finished, because all other bounding boxes in
  1469.      * the priority queue are further away.
  1470.      */
  1471.  
  1472.     if (Depth > Best)
  1473.     {
  1474.       break;
  1475.     }
  1476.  
  1477.     /* Check current node. */
  1478.  
  1479.     if (Node->Entries)
  1480.     {
  1481.       /* This is a node containing leaves to be checked. */
  1482.  
  1483.       for (i = 0; i < Node->Entries; i++)
  1484.       {
  1485.         Check_And_Enqueue(Mesh_Queue, Node->Node[i], &Node->Node[i]->BBox, &rayinfo);
  1486.       }
  1487.     }
  1488.     else
  1489.     {
  1490.       /* This is a leaf so test the contained triangle. */
  1491.  
  1492.       if (intersect_mesh_triangle(Ray, Mesh, (MESH_TRIANGLE *)Node->Node, &Depth))
  1493.       {
  1494.         if (test_hit((MESH_TRIANGLE *)Node->Node, Mesh, Orig_Ray, Depth / len, Depth_Stack))
  1495.         {
  1496.           found = TRUE;
  1497.  
  1498.           Best = Depth;
  1499.         }
  1500.       }
  1501.     }
  1502.   }
  1503.  
  1504.   return(found);
  1505. }
  1506.  
  1507.  
  1508.  
  1509. /*****************************************************************************
  1510. *
  1511. * FUNCTION
  1512. *
  1513. *   mesh_hash
  1514. *
  1515. * INPUT
  1516. *
  1517. *   aPoint - Normal/Vertex to store
  1518. *   
  1519. * OUTPUT
  1520. *
  1521. *   Hash_Table - Normal/Vertex hash table
  1522. *   Number     - Number of normals/vertices
  1523. *   Max        - Max. number of normals/vertices
  1524. *   Elements   - List of normals/vertices
  1525. *   
  1526. * RETURNS
  1527. *
  1528. *   int - Index of normal/vertex into the normals/vertices list
  1529. *   
  1530. * AUTHOR
  1531. *
  1532. *   Dieter Bayer
  1533. *   
  1534. * DESCRIPTION
  1535. *
  1536. *   Try to locate a triangle normal/vertex in the normal/vertex list.
  1537. *   If the vertex is not found its stored in the normal/vertex list.
  1538. *
  1539. * CHANGES
  1540. *
  1541. *   Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
  1542. *
  1543. ******************************************************************************/
  1544.  
  1545. static int mesh_hash(Hash_Table, Number, Max, Elements, aPoint)
  1546. HASH_TABLE **Hash_Table;
  1547. int *Number, *Max;
  1548. SNGL_VECT **Elements;
  1549. VECTOR aPoint;
  1550. {
  1551.   int hash;
  1552.   SNGL_VECT D, P;
  1553.   HASH_TABLE *p;
  1554.  
  1555.   Assign_SNGL_Vect(P, aPoint);
  1556.  
  1557.   /* Get hash value. */
  1558.  
  1559.   hash = (unsigned)((int)(326.0*P[X])^(int)(694.7*P[Y])^(int)(1423.6*P[Z])) % HASH_SIZE;
  1560.  
  1561.   /* Try to find normal/vertex. */
  1562.  
  1563.   for (p = Hash_Table[hash]; p != NULL; p = p->Next)
  1564.   {
  1565.     VSub(D, p->P, P);
  1566.  
  1567.     if ((fabs(D[X]) < EPSILON) && (fabs(D[Y]) < EPSILON) && (fabs(D[Z]) < EPSILON))
  1568.     {
  1569.       break;
  1570.     }
  1571.   }
  1572.  
  1573.   if ((p != NULL) && (p->Index >= 0))
  1574.   {
  1575.     return(p->Index);
  1576.   }
  1577.  
  1578.   /* Add new normal/vertex to the list and hash table. */
  1579.  
  1580.   if ((*Number) >= (*Max))
  1581.   {
  1582.     if ((*Max) >= INT_MAX/2)
  1583.     {
  1584.       Error("Too many normals/vertices in mesh.\n");
  1585.     }
  1586.  
  1587.     (*Max) *= 2;
  1588.  
  1589.     (*Elements) = (SNGL_VECT *)POV_REALLOC((*Elements), (*Max)*sizeof(SNGL_VECT), "mesh data");
  1590.   }
  1591.  
  1592.   Assign_SNGL_Vect((*Elements)[*Number], P);
  1593.  
  1594.   p = (HASH_TABLE *)POV_MALLOC(sizeof(HASH_TABLE), "mesh data");
  1595.  
  1596.   Assign_SNGL_Vect(p->P, P);
  1597.  
  1598.   p->Index = *Number;
  1599.  
  1600.   p->Next = Hash_Table[hash];
  1601.  
  1602.   Hash_Table[hash] = p;
  1603.  
  1604.   return((*Number)++);
  1605. }
  1606.  
  1607.  
  1608.  
  1609. /*****************************************************************************
  1610. *
  1611. * FUNCTION
  1612. *
  1613. *   Mesh_Hash_Vertex
  1614. *
  1615. * INPUT
  1616. *
  1617. *   Vertex - Vertex to store
  1618. *   
  1619. * OUTPUT
  1620. *
  1621. *   Number_Of_Vertices - Number of vertices
  1622. *   Max_Vertices       - Max. number of vertices
  1623. *   Vertices           - List of vertices
  1624. *   
  1625. * RETURNS
  1626. *
  1627. *   int - Index of vertex into the vertices list
  1628. *   
  1629. * AUTHOR
  1630. *
  1631. *   Dieter Bayer
  1632. *   
  1633. * DESCRIPTION
  1634. *
  1635. *   Try to locate a triangle vertex in the vertex list.
  1636. *   If the vertex is not found its stored in the vertex list.
  1637. *
  1638. * CHANGES
  1639. *
  1640. *   Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
  1641. *
  1642. ******************************************************************************/
  1643.  
  1644. int Mesh_Hash_Vertex(Number_Of_Vertices, Max_Vertices, Vertices, Vertex)
  1645. int *Number_Of_Vertices, *Max_Vertices;
  1646. SNGL_VECT **Vertices;
  1647. VECTOR Vertex;
  1648. {
  1649.   return(mesh_hash(Vertex_Hash_Table, Number_Of_Vertices, Max_Vertices, Vertices, Vertex));
  1650. }
  1651.  
  1652.  
  1653.  
  1654. /*****************************************************************************
  1655. *
  1656. * FUNCTION
  1657. *
  1658. *   Mesh_Hash_Normal
  1659. *
  1660. * INPUT
  1661. *
  1662. *   Normal - Normal to store
  1663. *   
  1664. * OUTPUT
  1665. *
  1666. *   Number_Of_Normals - Number of normals
  1667. *   Max_Normals       - Max. number of normals
  1668. *   Normals           - List of normals
  1669. *   
  1670. * RETURNS
  1671. *
  1672. *   int - Index of normal into the normals list
  1673. *   
  1674. * AUTHOR
  1675. *
  1676. *   Dieter Bayer
  1677. *   
  1678. * DESCRIPTION
  1679. *
  1680. *   Try to locate a triangle normal in the normal list.
  1681. *   If the normal is not found its stored in the normal list.
  1682. *
  1683. * CHANGES
  1684. *
  1685. *   Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
  1686. *
  1687. ******************************************************************************/
  1688.  
  1689. int Mesh_Hash_Normal(Number_Of_Normals, Max_Normals, Normals, S_Normal)
  1690. int *Number_Of_Normals, *Max_Normals;
  1691. SNGL_VECT **Normals;
  1692. VECTOR S_Normal;
  1693. {
  1694.   return(mesh_hash(Normal_Hash_Table, Number_Of_Normals, Max_Normals, Normals, S_Normal));
  1695. }
  1696.  
  1697.  
  1698.  
  1699. /*****************************************************************************
  1700. *
  1701. * FUNCTION
  1702. *
  1703. *   Mesh_Hash_Texture
  1704. *
  1705. * INPUT
  1706. *
  1707. *   Texture - Texture to store
  1708. *   
  1709. * OUTPUT
  1710. *
  1711. *   Number_Of_Textures - Number of textures
  1712. *   Max_Textures       - Max. number of textures
  1713. *   Textures           - List of textures
  1714. *   
  1715. * RETURNS
  1716. *
  1717. *   int - Index of texture into the texture list
  1718. *   
  1719. * AUTHOR
  1720. *
  1721. *   Dieter Bayer
  1722. *   
  1723. * DESCRIPTION
  1724. *
  1725. *   Try to locate a texture in the texture list.
  1726. *   If the texture is not found its stored in the texture list.
  1727. *
  1728. * CHANGES
  1729. *
  1730. *   Feb 1995 : Creation.
  1731. *
  1732. ******************************************************************************/
  1733.  
  1734. int Mesh_Hash_Texture(Number_Of_Textures, Max_Textures, Textures, Texture)
  1735. int *Number_Of_Textures, *Max_Textures;
  1736. TEXTURE ***Textures, *Texture;
  1737. {
  1738.   int i;
  1739.  
  1740.   if (Texture == NULL)
  1741.   {
  1742.     return(-1);
  1743.   }
  1744.  
  1745.   /* Just do a linear search. */
  1746.  
  1747.   for (i = 0; i < *Number_Of_Textures; i++)
  1748.   {
  1749.     if ((*Textures)[i] == Texture)
  1750.     {
  1751.       break;
  1752.     }
  1753.   }
  1754.  
  1755.   if (i == *Number_Of_Textures)
  1756.   {
  1757.     if ((*Number_Of_Textures) >= (*Max_Textures))
  1758.     {
  1759.       if ((*Max_Textures) >= INT_MAX/2)
  1760.       {
  1761.         Error("Too many textures in mesh.\n");
  1762.       }
  1763.  
  1764.       (*Max_Textures) *= 2;
  1765.  
  1766.       (*Textures) = (TEXTURE **)POV_REALLOC((*Textures), (*Max_Textures)*sizeof(TEXTURE *), "mesh data");
  1767.     }
  1768.  
  1769.     (*Textures)[(*Number_Of_Textures)++] = Copy_Texture_Pointer(Texture);
  1770.   }
  1771.  
  1772.   return(i);
  1773. }
  1774.  
  1775.  
  1776.  
  1777. /*****************************************************************************
  1778. *
  1779. * FUNCTION
  1780. *
  1781. *   Create_Mesh_Hash_Tables
  1782. *
  1783. * INPUT
  1784. *   
  1785. * OUTPUT
  1786. *   
  1787. * RETURNS
  1788. *   
  1789. * AUTHOR
  1790. *
  1791. *   Dieter Bayer
  1792. *   
  1793. * DESCRIPTION
  1794. *
  1795. *   -
  1796. *
  1797. * CHANGES
  1798. *
  1799. *   Feb 1995 : Creation.
  1800. *
  1801. ******************************************************************************/
  1802.  
  1803. void Create_Mesh_Hash_Tables()
  1804. {
  1805.   int i;
  1806.  
  1807.   Vertex_Hash_Table = (HASH_TABLE **)POV_MALLOC(HASH_SIZE*sizeof(HASH_TABLE *), "mesh hash table");
  1808.  
  1809.   for (i = 0; i < HASH_SIZE; i++)
  1810.   {
  1811.     Vertex_Hash_Table[i] = NULL;
  1812.   }
  1813.  
  1814.   Normal_Hash_Table = (HASH_TABLE **)POV_MALLOC(HASH_SIZE*sizeof(HASH_TABLE *), "mesh hash table");
  1815.  
  1816.   for (i = 0; i < HASH_SIZE; i++)
  1817.   {
  1818.     Normal_Hash_Table[i] = NULL;
  1819.   }
  1820. }
  1821.  
  1822.  
  1823.  
  1824. /*****************************************************************************
  1825. *
  1826. * FUNCTION
  1827. *
  1828. *   Destroy_Mesh_Hash_Tables
  1829. *
  1830. * INPUT
  1831. *   
  1832. * OUTPUT
  1833. *   
  1834. * RETURNS
  1835. *   
  1836. * AUTHOR
  1837. *
  1838. *   Dieter Bayer
  1839. *   
  1840. * DESCRIPTION
  1841. *
  1842. *   -
  1843. *
  1844. * CHANGES
  1845. *
  1846. *   Feb 1995 : Creation.
  1847. *
  1848. ******************************************************************************/
  1849.  
  1850. void Destroy_Mesh_Hash_Tables()
  1851. {
  1852.   int i;
  1853.   HASH_TABLE *Temp;
  1854.  
  1855.   for (i = 0; i < HASH_SIZE; i++)
  1856.   {
  1857.     while (Vertex_Hash_Table[i] != NULL)
  1858.     {
  1859.       Temp = Vertex_Hash_Table[i];
  1860.       
  1861.       Vertex_Hash_Table[i] = Temp->Next;
  1862.       
  1863.       POV_FREE(Temp);
  1864.     }
  1865.   }
  1866.  
  1867.   POV_FREE(Vertex_Hash_Table);
  1868.  
  1869.   for (i = 0; i < HASH_SIZE; i++)
  1870.   {
  1871.     while (Normal_Hash_Table[i] != NULL)
  1872.     {
  1873.       Temp = Normal_Hash_Table[i];
  1874.       
  1875.       Normal_Hash_Table[i] = Temp->Next;
  1876.       
  1877.       POV_FREE(Temp);
  1878.     }
  1879.   }
  1880.  
  1881.   POV_FREE(Normal_Hash_Table);
  1882. }
  1883.  
  1884.  
  1885.  
  1886. /*****************************************************************************
  1887. *
  1888. * FUNCTION
  1889. *
  1890. *   get_triangle_vertices
  1891. *
  1892. * INPUT
  1893. *
  1894. *   Mesh     - Mesh object
  1895. *   Triangle - Triangle
  1896. *   
  1897. * OUTPUT
  1898. *   
  1899. * RETURNS
  1900. *
  1901. *   P1, P2, P3 - Vertices of the triangle
  1902. *   
  1903. * AUTHOR
  1904. *
  1905. *   Dieter Bayer
  1906. *   
  1907. * DESCRIPTION
  1908. *
  1909. *   -
  1910. *
  1911. * CHANGES
  1912. *
  1913. *   Feb 1995 : Creation.
  1914. *
  1915. ******************************************************************************/
  1916.  
  1917. static void get_triangle_vertices(Mesh, Triangle, P1, P2, P3)
  1918. MESH *Mesh;
  1919. MESH_TRIANGLE *Triangle;
  1920. VECTOR P1, P2, P3;
  1921. {
  1922.   Assign_SNGL_Vect(P1, Mesh->Data->Vertices[Triangle->P1]);
  1923.   Assign_SNGL_Vect(P2, Mesh->Data->Vertices[Triangle->P2]);
  1924.   Assign_SNGL_Vect(P3, Mesh->Data->Vertices[Triangle->P3]);
  1925. }
  1926.  
  1927.  
  1928.  
  1929. /*****************************************************************************
  1930. *
  1931. * FUNCTION
  1932. *
  1933. *   get_triangle_normals
  1934. *
  1935. * INPUT
  1936. *
  1937. *   Mesh     - Mesh object
  1938. *   Triangle - Triangle
  1939. *   
  1940. * OUTPUT
  1941. *   
  1942. * RETURNS
  1943. *
  1944. *   N1, N2, N3 - Normals of the triangle
  1945. *   
  1946. * AUTHOR
  1947. *
  1948. *   Dieter Bayer
  1949. *   
  1950. * DESCRIPTION
  1951. *
  1952. *   -
  1953. *
  1954. * CHANGES
  1955. *
  1956. *   Feb 1995 : Creation.
  1957. *
  1958. ******************************************************************************/
  1959.  
  1960. static void get_triangle_normals(Mesh, Triangle, N1, N2, N3)
  1961. MESH *Mesh;
  1962. MESH_TRIANGLE *Triangle;
  1963. VECTOR N1, N2, N3;
  1964. {
  1965.   Assign_SNGL_Vect(N1, Mesh->Data->Normals[Triangle->N1]);
  1966.   Assign_SNGL_Vect(N2, Mesh->Data->Normals[Triangle->N2]);
  1967.   Assign_SNGL_Vect(N3, Mesh->Data->Normals[Triangle->N3]);
  1968. }
  1969.  
  1970.  
  1971.  
  1972. /*****************************************************************************
  1973. *
  1974. * FUNCTION
  1975. *
  1976. *   Mesh_Degenerate
  1977. *
  1978. * INPUT
  1979. *
  1980. *   P1, P2, P3 - Triangle's vertices
  1981. *   
  1982. * OUTPUT
  1983. *   
  1984. * RETURNS
  1985. *
  1986. *   int - TRUE if degenerate
  1987. *   
  1988. * AUTHOR
  1989. *
  1990. *   Dieter Bayer
  1991. *   
  1992. * DESCRIPTION
  1993. *
  1994. *   Test if a triangle is degenerate.
  1995. *
  1996. * CHANGES
  1997. *
  1998. *   Feb 1995 : Creation.
  1999. *
  2000. ******************************************************************************/
  2001.  
  2002. int Mesh_Degenerate(P1, P2, P3)
  2003. VECTOR P1, P2, P3;
  2004. {
  2005.   VECTOR V1, V2, Temp;
  2006.   DBL Length;
  2007.  
  2008.   VSub(V1, P1, P2);
  2009.   VSub(V2, P3, P2);
  2010.  
  2011.   VCross(Temp, V1, V2);
  2012.  
  2013.   VLength(Length, Temp);
  2014.  
  2015.   return(Length == 0.0);
  2016. }
  2017.  
  2018.  
  2019. /*****************************************************************************
  2020. *
  2021. * FUNCTION
  2022. *
  2023. *   Initialize_Mesh_Code
  2024. *
  2025. * INPUT
  2026. *   
  2027. * OUTPUT
  2028. *   
  2029. * RETURNS
  2030. *   
  2031. * AUTHOR
  2032. *
  2033. *   Dieter Bayer
  2034. *   
  2035. * DESCRIPTION
  2036. *
  2037. *   Initialize mesh specific variables.
  2038. *
  2039. * CHANGES
  2040. *
  2041. *   Jul 1995 : Creation.
  2042. *
  2043. ******************************************************************************/
  2044.  
  2045. void Initialize_Mesh_Code()
  2046. {
  2047.   Mesh_Queue = Create_Priority_Queue(INITIAL_NUMBER_OF_ENTRIES);
  2048. }
  2049.  
  2050.  
  2051.  
  2052. /*****************************************************************************
  2053. *
  2054. * FUNCTION
  2055. *
  2056. *   Deinitialize_Mesh_Code
  2057. *
  2058. * INPUT
  2059. *   
  2060. * OUTPUT
  2061. *   
  2062. * RETURNS
  2063. *   
  2064. * AUTHOR
  2065. *
  2066. *   Dieter Bayer
  2067. *   
  2068. * DESCRIPTION
  2069. *
  2070. *   Deinitialize mesh specific variables.
  2071. *
  2072. * CHANGES
  2073. *
  2074. *   Jul 1995 : Creation.
  2075. *
  2076. ******************************************************************************/
  2077.  
  2078. void Deinitialize_Mesh_Code()
  2079. {
  2080.   if (Mesh_Queue != NULL)
  2081.   {
  2082.     Destroy_Priority_Queue(Mesh_Queue);
  2083.   }
  2084.  
  2085.   Mesh_Queue = NULL;
  2086. }
  2087.  
  2088.  
  2089.  
  2090. /*****************************************************************************
  2091. *
  2092. * FUNCTION
  2093. *
  2094. *   Test_Mesh_Opacity
  2095. *
  2096. * INPUT
  2097. *
  2098. *   Mesh - Pointer to mesh structure
  2099. *
  2100. * OUTPUT
  2101. *
  2102. *   Mesh
  2103. *
  2104. * RETURNS
  2105. *
  2106. * AUTHOR
  2107. *
  2108. *   Dieter Bayer
  2109. *
  2110. * DESCRIPTION
  2111. *
  2112. *   Set the opacity flag of the mesh according to the opacity
  2113. *   of the mesh's texture(s).
  2114. *
  2115. * CHANGES
  2116. *
  2117. *   Apr 1996 : Creation.
  2118. *
  2119. ******************************************************************************/
  2120.  
  2121. void Test_Mesh_Opacity(Mesh)
  2122. MESH *Mesh;
  2123. {
  2124.   int i;
  2125.  
  2126.   /* Initialize opacity flag to the opacity of the object's texture. */
  2127.  
  2128.   if ((Mesh->Texture == NULL) || (Test_Opacity(Mesh->Texture)))
  2129.   {
  2130.     Set_Flag(Mesh, OPAQUE_FLAG);
  2131.   }
  2132.  
  2133.   if (Test_Flag(Mesh, MULTITEXTURE_FLAG))
  2134.   {
  2135.     for (i = 0; i < Mesh->Data->Number_Of_Textures; i++)
  2136.     {
  2137.       if (Mesh->Data->Textures[i] != NULL)
  2138.       {
  2139.         /* If component's texture isn't opaque the mesh is neither. */
  2140.  
  2141.         if (!Test_Opacity(Mesh->Data->Textures[i]))
  2142.         {
  2143.           Clear_Flag(Mesh, OPAQUE_FLAG);
  2144.         }
  2145.       }
  2146.     }
  2147.   }
  2148. }
  2149.  
  2150.  
  2151.  
  2152.